package prefuse.tp.svn;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JToolBar;
import javax.swing.border.BevelBorder;

import prefuse.Constants;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataColorAction;
import prefuse.action.assignment.FontAction;
import prefuse.action.layout.RandomLayout;
import prefuse.action.layout.graph.BalloonTreeLayout;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.action.layout.graph.FruchtermanReingoldLayout;
import prefuse.action.layout.graph.NodeLinkTreeLayout;
import prefuse.action.layout.graph.RadialTreeLayout;
import prefuse.action.layout.graph.SquarifiedTreeMapLayout;
import prefuse.render.DefaultRendererFactory;
import prefuse.util.ColorLib;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.InGroupPredicate;

public class GraphRadial_jMenu extends JMenuBar{
	
	private static final long serialVersionUID = 1L;
	public JMenuBar menuBar;
	public JToolBar toolBar;
	
	public GraphRadial_jMenu (){
		super();
		 setBorder(new BevelBorder(BevelBorder.RAISED));
		 ActionListener menuListener = new MenuActionListener();
		 
		 JMenu menu = new JMenu("File");
		 add(menu);
		 JMenuItem 		Item_QUIT = new JMenuItem("Close", KeyEvent.VK_C);
		 Item_QUIT.addActionListener(menuListener);
		 menu.add(Item_QUIT);
		 
		 JMenu menu_Edit = new JMenu("Edit");
		 add(menu_Edit);
		 JMenu 		Item_Layout = new JMenu("Layout");
		 menu_Edit.add(Item_Layout);
		 
		 JMenuItem 		Item_Pref_grfl = new JMenuItem("GraphRadial_FlexibleLayout");
		 Item_Pref_grfl.addActionListener(menuListener);
		 JMenuItem 		Item_Pref_rtl = new JMenuItem("RadialTreeLayout");
		 Item_Pref_rtl.addActionListener(menuListener);
		 JMenuItem 		Item_Pref_btl = new JMenuItem("BalloonTreeLayout");
		 Item_Pref_btl.addActionListener(menuListener);
		 JMenuItem 		Item_Pref_stl = new JMenuItem("SquarifiedTreeMapLayout");
		 Item_Pref_stl.addActionListener(menuListener);
		 JMenuItem 		Item_Pref_nltl = new JMenuItem("NodeLinkTreeLayout");
		 Item_Pref_nltl.addActionListener(menuListener);
		 JMenuItem 		Item_Pref_frl = new JMenuItem("FruchtermanReingoldLayout");
		 Item_Pref_frl.addActionListener(menuListener);
		 JMenuItem 		Item_Pref_rl = new JMenuItem("RandomLayout");
		 Item_Pref_rl.addActionListener(menuListener);
		 JMenuItem 		Item_Pref_fdl = new JMenuItem("ForceDirectedLayout");
		 Item_Pref_fdl.addActionListener(menuListener);
		 
		 Item_Layout.add(Item_Pref_grfl);
		 Item_Layout.add(Item_Pref_rtl);
		 Item_Layout.add(Item_Pref_btl);
		 Item_Layout.add(Item_Pref_stl);
		 Item_Layout.add(Item_Pref_nltl);
		 Item_Layout.add(Item_Pref_frl);
		 Item_Layout.add(Item_Pref_rl);
		 Item_Layout.add(Item_Pref_fdl);
		 
		 JMenuItem 		Item_Pref_f = new JMenuItem("Change Font", KeyEvent.VK_P);
		 Item_Pref_f.addActionListener(menuListener);
		 menu_Edit.add(Item_Pref_f);
		 JMenuItem 		Item_Pref_n = new JMenuItem("Change Node", KeyEvent.VK_P);
		 Item_Pref_n.addActionListener(menuListener);
		 menu_Edit.add(Item_Pref_n);

	}
	
	static class MenuActionListener implements ActionListener {
	    public void actionPerformed(ActionEvent actionEvent) {
	      if (actionEvent.getActionCommand()=="Close"){
	    	  System.exit(0);
	      }
	      if (actionEvent.getActionCommand()=="GraphRadial_FlexibleLayout"){
	    	GraphRadial.filter.remove(GraphRadial.treeLayout);
	    	GraphRadial.treeLayout = new GraphRadial_FlexibleLayout(GraphRadial.tree);
	    	GraphRadial.filter.add(GraphRadial.treeLayout);
	    	GraphRadial.vis.putAction("filter", GraphRadial.filter);
	    	GraphRadial.vis.run("filter");  
	      }
	      if (actionEvent.getActionCommand()=="RadialTreeLayout"){
		    	GraphRadial.filter.remove(GraphRadial.treeLayout);
		    	GraphRadial.treeLayout = new RadialTreeLayout(GraphRadial.tree);
		    	GraphRadial.filter.add(GraphRadial.treeLayout);
		    	GraphRadial.vis.putAction("filter", GraphRadial.filter);
		    	GraphRadial.vis.run("filter");  
		  }
	      if (actionEvent.getActionCommand()=="BalloonTreeLayout"){
		    	GraphRadial.filter.remove(GraphRadial.treeLayout);
		    	GraphRadial.treeLayout = new BalloonTreeLayout(GraphRadial.tree);
		    	GraphRadial.filter.add(GraphRadial.treeLayout);
		    	GraphRadial.vis.putAction("filter", GraphRadial.filter);
		    	GraphRadial.vis.run("filter");  
		  }
	      if (actionEvent.getActionCommand()=="SquarifiedTreeMapLayout"){
		    	GraphRadial.filter.remove(GraphRadial.treeLayout);
		    	GraphRadial.treeLayout = new SquarifiedTreeMapLayout(GraphRadial.tree);
		    	GraphRadial.filter.add(GraphRadial.treeLayout);
		    	GraphRadial.vis.putAction("filter", GraphRadial.filter);
		    	GraphRadial.vis.run("filter");  
		  }
	      if (actionEvent.getActionCommand()=="NodeLinkTreeLayout"){
		    	GraphRadial.filter.remove(GraphRadial.treeLayout);
		    	GraphRadial.treeLayout = new NodeLinkTreeLayout(GraphRadial.tree);
		    	GraphRadial.filter.add(GraphRadial.treeLayout);
		    	GraphRadial.vis.putAction("filter", GraphRadial.filter);
		    	GraphRadial.vis.run("filter");  
		  }
	      if (actionEvent.getActionCommand()=="FruchtermanReingoldLayout"){
		    	GraphRadial.filter.remove(GraphRadial.treeLayout);
		    	GraphRadial.treeLayout = new FruchtermanReingoldLayout(GraphRadial.tree);
		    	GraphRadial.filter.add(GraphRadial.treeLayout);
		    	GraphRadial.vis.putAction("filter", GraphRadial.filter);
		    	GraphRadial.vis.run("filter");  
		  }
	      if (actionEvent.getActionCommand()=="RandomLayout"){
		    	GraphRadial.filter.remove(GraphRadial.treeLayout);
		    	GraphRadial.treeLayout = new RandomLayout(GraphRadial.tree);
		    	GraphRadial.filter.add(GraphRadial.treeLayout);
		    	GraphRadial.vis.putAction("filter", GraphRadial.filter);
		    	GraphRadial.vis.run("filter");  
		  }
	      if (actionEvent.getActionCommand()=="ForceDirectedLayout"){
		    	GraphRadial.filter.remove(GraphRadial.treeLayout);
		    	GraphRadial.treeLayout = new ForceDirectedLayout(GraphRadial.tree);
		    	GraphRadial.filter.add(GraphRadial.treeLayout);
		    	GraphRadial.vis.putAction("filter", GraphRadial.filter);
		    	GraphRadial.vis.run("filter");  
		  }
	      if (actionEvent.getActionCommand()=="Change Font"){
	    	  final GraphRadial_FontChooser chooser = new GraphRadial_FontChooser(GraphRadial.frame);
	    	  chooser.setVisible(true);
	    	  //    	  System.out.println("_______________________________________");
	    	  if(chooser.getNewFont() != null) {
//	    		  System.out.println(chooser.getNewFont());
		    	  GraphRadial.filter.remove(GraphRadial.fonts);
		    	  GraphRadial.fonts = new FontAction(GraphRadial.treeNodes, chooser.getNewFont());
		    	  GraphRadial.fonts.add("_hover", chooser.getNewFont());
		    	  GraphRadial.fonts.add("ingroup('_focus_')", chooser.getNewFont());
		    	  GraphRadial.fonts.add("ingroup('_search_')", chooser.getNewFont());
		    	  GraphRadial.filter.add(GraphRadial.fonts);
		    	  GraphRadial.vis.putAction("filter", GraphRadial.filter);
		    	  GraphRadial.vis.alwaysRunAfter("filter", "animate");
		    	  GraphRadial.textColor.setDefaultColor(chooser.getNewColor().getRGB());
			      GraphRadial.vis.run("filter");
		      }  
	      }
	      if (actionEvent.getActionCommand()=="Change Node"){
	    	  final GraphRadial_NodeColorChooser chooser = new GraphRadial_NodeColorChooser(GraphRadial.frame);
	    	  chooser.setVisible(true);
	    	  GraphRadial.filter.remove(GraphRadial.dataColorUE);
	    	  GraphRadial.m_nodeRenderer.setRenderType(chooser.getNewRender());
	    	  int[] color = {
	    			  ColorLib.rgb(
	    					  chooser.getNewColor().getRed(),
	    					  chooser.getNewColor().getGreen(),
	    					  chooser.getNewColor().getBlue()
	    					  ),
	    			  ColorLib.rgb(
	    					  chooser.getNewColor().getRed()*9/10,
	    					  chooser.getNewColor().getGreen()*9/10,
	    					  chooser.getNewColor().getBlue()*9/10
	    					  ),
	    			  ColorLib.rgb(
	    					  chooser.getNewColor().getRed()*8/10,
	    					  chooser.getNewColor().getGreen()*8/10,
	    					  chooser.getNewColor().getBlue()*8/10
	    					  ),
	    			  ColorLib.rgb(
	    					  chooser.getNewColor().getRed()*7/10,
	    					  chooser.getNewColor().getGreen()*7/10,
	    					  chooser.getNewColor().getBlue()*7/10
	    					  )};
	    	  
	    	  ColorAction dataColor = new DataColorAction(GraphRadial.treeNodes,
						"Mention",
						Constants.NOMINAL,
						VisualItem.FILLCOLOR,
						color);
	    	  
	    	  DefaultRendererFactory rf = new DefaultRendererFactory(GraphRadial.m_nodeRenderer);
	          rf.add(new InGroupPredicate(GraphRadial.treeEdges),GraphRadial.m_edgeRenderer);
	          GraphRadial.vis.setRendererFactory(rf);
	          GraphRadial.filter.add(dataColor);
	          
	          ActionList repaint = new ActionList();
	          repaint.add(GraphRadial.filter);
	          repaint.add(new RepaintAction());
	          
	          GraphRadial.vis.putAction("repaint", repaint);
	    	  GraphRadial.vis.run("filter");
	      }
	    }
	}
	
}